<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * This object will represent xml similar to this:
 * 
 * <Link rel="lrdd" template="http://identi.ca/main/xrd?uri={uri}">
 *   <Title>Resource Descriptor</Title>
 * </Link>
 * 
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */

class Xrd_HostMeta_TemplateLink implements Xrd_ChildElement {

    const ELEMENT_LINK       = "Link";
    
    const ATTRIBUTE_REL      = "rel";
    const ATTRIBUTE_TEMPLATE = "template";
    
    const ATTRIBUTE_REL_VALUE_LRDD = "lrdd";
    
    private $rel;
    private $template;
    
    private $title;
    
    private function __construct($rel, $template) {
        $this->setRel($rel);
        $this->setTemplate($template);
    } 
    
    public function setRel($rel) {
        if(empty($rel)) {
            throw new Exception("Rel cannot be set empty!");
        }
        $this->rel = $rel;
    }
    
    public function setTitle(Xrd_HostMeta_TemplateLink_Title $title) {
        if(empty($title)) {
            throw new Exception("Title cannot be set empty!");
        }
        $this->title = $title;
    }
    
    public function setTemplate($template) {
        if(empty($template)) {
            throw new Exception("Template cannot be set empty!");
        }
        $this->template = $template;
    }
    
    public function getTemplate() {
        return $this->template;
    }
    
    
    public function asDOMElement(DOMDocument $dom) {
        $link = $dom->createElement(self::ELEMENT_LINK);
        $link->setAttribute(self::ATTRIBUTE_REL, self::ATTRIBUTE_REL_VALUE_LRDD);
        $link->setAttribute(self::ATTRIBUTE_TEMPLATE, $this->template);
        
        $link->appendChild($this->title->asDomElement($dom));
        
        return $link;
    }
    
    /**
     * <Link rel="lrdd" template="http://identi.ca/main/xrd?uri={uri}">
     *   <Title>Resource Descriptor</Title>
     * </Link>
     * 
     * @param DOMElement $elm
     * @return Xrd_HostMeta_TemplateLink 
     */
    public static function buildFromDOMElement(DOMElement $elm) {
        $templateLink = new Xrd_HostMeta_TemplateLink( $elm->getAttribute(self::ATTRIBUTE_REL), $elm->getAttribute(self::ATTRIBUTE_TEMPLATE) );
        $title = Xrd_HostMeta_TemplateLink_Title::buildFromDOMElement($elm->getElementsByTagName(Xrd_HostMeta_TemplateLink_Title::ELEMENT_TITLE)->item(0));
        $templateLink->setTitle($title);
        return $templateLink;
    }
    
    public static function buildXrdHostMetaLink($tempateURI) {
        
        $templateLink = new Xrd_HostMeta_TemplateLink( self::ATTRIBUTE_REL_VALUE_LRDD, $tempateURI );
        
        $title = new Xrd_HostMeta_TemplateLink_Title(Xrd_HostMeta_TemplateLink_Title::VALUE_TITLE);
        $templateLink->setTitle($title);
        
        return $templateLink;
    }
}